package com.jack.vipmis.vip.member.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.vipmis.common.entity.enums.BooleanEnum;
import com.jack.vipmis.common.web.controller.BaseCRUDController;
import com.jack.vipmis.common.web.validate.ValidateResponse;
import com.jack.vipmis.vip.member.entity.Member;
import com.jack.vipmis.vip.member.service.MemberService;

/**
 * 会员信息控制器
 * 
 * @author JackLiang
 * @date 2014-7-4
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/vip/member")
public class MemberController extends BaseCRUDController<Member, Long> {

	private MemberService getMemberService() {
		return (MemberService) baseService;
	}

	public MemberController() {
		setResourceIdentity("vip:member");
	}

	@Override
	protected void setCommonData(Model model) {
		model.addAttribute("booleanList", BooleanEnum.values());
	}

	/**
	 * 验证返回格式 单个：[fieldId, 1|0, msg] 多个：[[fieldId, 1|0, msg],[fieldId, 1|0,
	 * msg]]
	 * 
	 * @param fieldId
	 * @param fieldValue
	 * @return
	 */
	@RequestMapping(value = "validate", method = RequestMethod.GET)
	@ResponseBody
	public Object validate(@RequestParam("fieldId") String fieldId, @RequestParam("fieldValue") String fieldValue,
			@RequestParam(value = "id", required = false) Long id) {
		ValidateResponse response = ValidateResponse.newInstance();

		if ("name".equals(fieldId)) {
			Member member = getMemberService().findByFirstName(fieldValue);
			if (member == null || (member.getId().equals(id) && member.getFirstName().equals(fieldValue))) {
				// 如果msg 不为空 将弹出提示框
				response.validateSuccess(fieldId, "");
			} else {
				response.validateFail(fieldId, "该名称已被其他人使用");
			}
		}
		return response.result();
	}
}
